/**
 * 
 */
package com.shine.cotrun.exp;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * 导出EXCEL
 * @author zsy
 *
 */
public class ExportToExcel<D> extends Export<D> {

	private HSSFWorkbook wb;
	private HSSFSheet sheet;
	private int rowNum;
	
	HSSFCellStyle floatStyle;
	HSSFCellStyle dateStyle;
	
	public ExportToExcel(ExportConf conf) {
		super(conf);
		this.extName = ".xls";
	}
	
	@Override
	public void init() {
		super.init();
		//创建一个工作簿
		wb = new HSSFWorkbook();
		//创建一个Sheet
		sheet = wb.createSheet(conf.getExpFileName());
		//创建样式
		floatStyle = wb.createCellStyle();
		HSSFDataFormat format = wb.createDataFormat();
		floatStyle.setDataFormat(format.getFormat("0.00"));
		dateStyle = wb.createCellStyle();
		format = wb.createDataFormat();
		dateStyle.setDataFormat(format.getFormat("yyyy-MM-dd"));
		
		HSSFCellStyle cellStyle_Bold = wb.createCellStyle();
		HSSFFont font = wb.createFont();   
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);   
        cellStyle_Bold.setFont(font);
		//写入头部信息
		HSSFRow row = sheet.createRow(rowNum++); // 创建第1行，也就是输出表头
		HSSFCell cell = null;
		for (int i = 0 ; i < conf.getColumnSize(); i++) {
			cell = row.createCell((short)i);// 创建第i列
			cell.setCellStyle(cellStyle_Bold);
			cell.setCellValue(new HSSFRichTextString(conf.getField(i).getExpColumnName()));
		}
	}
	
	@Override
	public void export(Collection<D> data) throws Exception {
		if (data == null || data.size() == 0) {
			return;
		}
		HSSFRow row = null;
		HSSFCell cell = null;
		for (Object o : data) {
			Class<?> c = o.getClass();
			row = sheet.createRow(rowNum++);
			for (int i = 0 ; i < conf.getColumnSize(); i++) {
				Method m = c.getMethod(conf.toMethod(conf.getField(i).getExpPropertie()));
				cell = row.createCell((short)i);// 创建第i列
				Object val = m.invoke(o);
				if (val instanceof Long || val instanceof Integer
						|| val instanceof Short || val instanceof Byte) {
					cell.setCellValue(( (Number)val).longValue());
				} else if (val instanceof Number) {
					//设置两位小数
					cell.setCellStyle(floatStyle);
					cell.setCellValue(( (Number)val).doubleValue());
				} else if (val instanceof Date) {
					
					cell.setCellStyle(dateStyle);
					cell.setCellValue((Date)val);
				} else {
					HSSFRichTextString sVal = new HSSFRichTextString(val == null ? "" : val.toString());
					cell.setCellValue(sVal);
				}
				
			}
		}
	}
	
	@Override
	public String end() {
		//自适应列宽
		for (int i = 0 ; i < conf.getColumnSize(); i++) {
			sheet.autoSizeColumn((short)i);
		}
		try {
			wb.write(outputStream);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return super.end();
		
	}

}
